// Copyright (c) 2008 Michael Buckley

// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:

// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#import <BTLSockets/BTLSockets.h>

#import "BTLSocketHandlerTests.h"
#import "BTLTestSocketHandler.h"

@implementation BTLSocketHandlerTests

- (void)testHandlers
{
	BTLTestSocketHandler* handler1 = [BTLTestSocketHandler new];
	BTLSocketIPAddress* addr = [BTLSocketIPAddress addressWithHostname:@"localhost" port:0 family:AF_INET error:nil];
	
	STAssertFalse([handler1 connectionOpened], nil);
	[handler1 connectionOpenedToAddress:addr];
	STAssertTrue([handler1 connectionOpened], nil);
	[handler1 closeConnectionToAddress:addr];
	STAssertFalse([handler1 connectionOpened], nil);
	[handler1 connectionOpenedToAddress:addr];
	STAssertTrue([handler1 connectionOpened], nil);
	[handler1 connectionClosedByRemoteAddress:addr];
	STAssertFalse([handler1 connectionOpened], nil);
	
	[handler1 connectionInterruptedToAddress:addr];
	STAssertTrue([handler1 connectionInterrupted], nil);
	
	[handler1 connectionFailedToAddress:addr];
	STAssertTrue([handler1 connectionFailed], nil);
	
	BTLTestSocketHandler* handler2 = [BTLTestSocketHandler new];
	[handler1 setNextHandler:handler2];
	[handler2 setPrevHandler:handler1];
	
	BTLSocketBuffer* data = [BTLSocketBuffer new];
	[data addData:"Test String" ofSize:12];
	[handler1 readData:data fromAddress:addr];
	
	STAssertTrue([handler1 readData], nil);
	STAssertFalse([handler2 readData], nil);
	
	[handler1 readData:data fromAddress:addr];
	STAssertTrue([handler2 readData], nil);
	
	BTLSocketBuffer* result = [handler2 lastRead];
	if(strcmp([result rawData], [data rawData]) != 0){
		STFail(@"Read data not equal");
	}
	if(![addr isEqual:[handler2 readAddress]]){
		STFail(@"Read address not equal");
	}
	
	[handler2 writeData:data toAddress:addr];
	STAssertTrue([handler2 wroteData], nil);
	STAssertFalse([handler1 wroteData], nil);
	
	[handler2 writeData:data toAddress:addr];
	STAssertTrue([handler1 wroteData], nil);
	
	result = [handler1 lastWrote];
	if(strcmp([result rawData], [data rawData]) != 0){
		STFail(@"Write data not equal");
	}
	if(![addr isEqual:[handler1 writeAddress]]){
		STFail(@"Write address not equal");
	}
}

@end
